package com.lanou.sfu.songforu.music_library_fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.SongMenuGridViewAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.entity.SongMenuEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;
import com.lanou.sfu.songforu.tools.SwipeRefreshLoadingLayout;

import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class SongMenuFragment extends BaseFragment implements SwipeRefreshLoadingLayout.OnRefreshListener,SwipeRefreshLoadingLayout.OnLoadListener {
    private View v;
    private VolleySingleton requestQueue;
    private GridView gridView;
    private Context context;
    private StringRequest request;
    private List<SongMenuEntity.ContentEntity> contentEntityList;
    private List<SongMenuEntity.ContentEntity> contentEntityListLoad;
    private int times = 1;
    private SongMenuGridViewAdapter adapter;
    private SwipeRefreshLoadingLayout swipeRefreshLoadingLayout;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.viewpager_songmenu, null);
        gridView = (GridView) v.findViewById(R.id.songsmenu_gridView);
        swipeRefreshLoadingLayout = (SwipeRefreshLoadingLayout) v.findViewById(R.id.songmenu_refreshLoading);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();
        requestQueue = VolleySingleton.getInstance(context);
        swipeRefreshLoadingLayout.setOnLoadListener(this);
        swipeRefreshLoadingLayout.setOnRefreshListener(this);
        String url = Url.SONG_MENU;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SongMenuEntity songMenuEntity = gson.fromJson(response, SongMenuEntity.class);
                contentEntityList = songMenuEntity.getContent();
                adapter = new SongMenuGridViewAdapter(context,contentEntityList);
                gridView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.addQueue(request);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("listId", contentEntityList.get(position).getListid());
                SongMenuDetailFragment fragment = new SongMenuDetailFragment();
                fragment.setArguments(bundle);
                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fl_main, fragment).addToBackStack(null).commit();
            }
        });
    }

    @Override
    public void onLoad() {
        times++;
        String url = Url.SONG_MENU + times;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SongMenuEntity songMenuEntity = gson.fromJson(response, SongMenuEntity.class);
                contentEntityListLoad = songMenuEntity.getContent();
                contentEntityList.addAll(contentEntityListLoad);
                adapter.addDatas(contentEntityList);
                swipeRefreshLoadingLayout.setLoading(false);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.addQueue(request);

    }

    @Override
    public void onRefresh() {
        String url = Url.SONG_MENU;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SongMenuEntity songMenuEntity = gson.fromJson(response, SongMenuEntity.class);
                List<SongMenuEntity.ContentEntity> contentEntityListRefresh = songMenuEntity.getContent();
                SongMenuGridViewAdapter adapter = new SongMenuGridViewAdapter(context,contentEntityListRefresh);
                gridView.setAdapter(adapter);
                swipeRefreshLoadingLayout.setRefreshing(false);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.addQueue(request);
        adapter.notifyDataSetChanged();

    }
}
